<?php

Yii::import('application.models._base.BaseCustomer');


class Customer extends BaseCustomer
{
	public $keywords;
	public $verifyCode;
	public $is_agree;
	public $conemail;
	public $conpassword;
	public $oldpass;
	public $newpass;
	public $conpass;
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	
	public function rules() {
		return array(
			array('user_name, password, email, created, status,conemail','required'),
		    array('is_agree','required','message'=>'You must agree to Terms and Conditions'),
		    array('conpassword','required','message'=>'Password cannot be blank.'),
			array('user_name','unique','className'=>'Customer'),
			array('email', 'unique','className'=>'Customer'),
			array('password','length','min'=>6),
			array('conpassword', 'compare', 'compareAttribute'=>'password'),
			array('user_name, password', 'length', 'max'=>45),
			array('email', 'email'),
			array('conemail', 'compare', 'compareAttribute'=>'email'),
			array('verifyCode', 'captcha',  'allowEmpty'=> !extension_loaded('gd')),
			array('id,keywords,user_name, password, email, created, status', 'safe', 'on'=>'search'),
		    array('status', 'safe', 'on'=>'frozen'),
			array('is_agree','in','range'=>array(1),'message'=>'please agree the privacy policy!'),
		);
	}	
// 	public function beforeSave(){
// 		$this->password=md5($this->password);
// 		return true;
// 	}
	public function beforeSave(){
	    $salt=$this->create_salt();
	    $this->salt=$salt;
	    $this->password=$this->ppb_password_hash($this->password, $salt);
	    return true;
	}
	function ppb_password_hash ($password, $salt)
	{
	    return md5(md5($password) . $salt);
	}
	function create_salt()
	{
	    $rand = md5(rand(2, 99999999));
	    $output = substr($rand, 0, 3);
	
	    return $output;
	}
	public function attributeLabels() {
		return array(
				'id' => Yii::t('app', 'ID'),
				'user_name' => Yii::t('app', 'User Name'),
				'password' => Yii::t('app', 'Create Password'),
				'conpassword' => Yii::t('app', 'Verify Password'),
				'email' => Yii::t('app', 'Email'),
				'created' => Yii::t('app', 'Created'),
				'status' => Yii::t('app', 'Status'),
		);
	}
	
	public function search() {
		$criteria = new CDbCriteria;
		if(!empty($this->keywords)){
			$criteria->compare('user_name', $this->keywords, true,'OR');
			$criteria->compare('email', $this->keywords, true,'OR');
		}
		return new CActiveDataProvider($this, array(
				'criteria' => $criteria,
		));
	}
		
}